import {
    findOutApply,
    getGoodsByIdFromOutApply,
    approveOutApply,
} from "../../../api/afftersale/refacout";


let outApplyApprove = {
    data: function () {
        return {
            search: {
                currentPage: 1,
                pageSize: 10,
                //创建时间
                startTime: "",
                endTime: "",

                //审核时间
                appStartTime: "",
                appEndTime: "",

                odoCode: "",//出库单号
                status: "",//返厂出库标志
                approveStatus: "",//审核状态
            },

            //日期组件
            createTime: [],
            appTime: [],

            //申请单
            applyList: [],

            //分页组件
            total: 0,
            currentPage: 1,
            pageSize: 10,

            //模态框可见性
            dialogTableVisible: false,
            //申请单对应的商品列表
            goodList: [],
            //申请单信息
            comments: "",
            refacLocation: "",
            refacReason: "",
            odoCode:"",
            createBy:"",

            //审核信息
            approverTime: "",
            approverComments: "",
            approveStatus: "",
            approver: "",//审核人
            formatApproveTime: "",

            type: "",//0 审核； 1 审核跟踪

            //提交信息
            approveInfo: {
                id: "",
                approveStatus: "",
                approverComments: "",
                createBy:"",
                odoCode:"",
            }
        }
    },
    methods: {
        //每页条数变化
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.search.currentPage = this.currentPage;
            this.search.pageSize = pageSize;
            this.find(this.search);
        },
        //页码改变
        handleCurrentChange(currentPage) {
            this.currentPage = currentPage;
            this.search.currentPage = currentPage;
            this.search.pageSize = this.pageSize;
            this.find(this.search);
        },

        //制单日期
        chooseTime() {
            this.search.startTime = this.createTime[0];
            this.search.endTime = this.createTime[1];
        },

        //审核日期
        applyChooseTime() {
            this.search.appStartTime = this.appTime[0];
            this.search.appEndTime = this.appTime[1];
        },

        //重置
        resetForm() {
            this.search = {
                currentPage: 1,
                pageSize: 5,
                //创建时间
                startTime: "",
                endTime: "",

                //审核时间
                appStartTime: "",
                appEndTime: "",

                odoCode: "",//出库单号
                status: "",//返厂出库标志
                approveStatus: "",//审核状态
                createBy: "",//制单人
            };
            //日期组件
            this.createTime = [];
            this.appTime = [];
            this.find();
        },

        //搜索
        findPage() {
            this.find(this.search);
        },

        //查询
        find(params) {
            let vm = this;
            if (params == null) {
                params = {
                    currentPage: vm.$data.currentPage,
                    pageSize: vm.$data.pageSize,
                }
            }
            findOutApply(params).then(value => {
                vm.applyList = value.records;
                vm.total = value.total;
            })
        },

        //时间格式化
        creatFormatter(row) {
            if (row.createTime !== null) {
                return row.createTime.replace("T", " ");
            }
        },
        approveFormatter(row) {
            if (row.approverTime !== null) {
                return row.approverTime.replace("T", " ");
            }
        },

        //查询已审核
        searchApproved() {
            this.search.approveStatus = 2;
            this.find(this.search);
        },
        //查询未审核
        searchToApprove() {
            this.search.approveStatus = 1;
            this.find(this.search);
        },
        //查询未通过
        searchReject() {
            this.search.approveStatus = 0;
            this.find(this.search);
        },

        //获得对应的商品信息进行审核
        showInfo(row) {
            this.dialogTableVisible = true;
            this.type = 0;
            this.approveInfo.id = row.id;
            this.createBy = row.createBy;
            this.odoCode = row.odoCode;
            this.approverComments = "";
            this.comments = row.comments;
            this.refacReason = row.refacReason;
            this.refacLocation = row.refacLocation;
            getGoodsByIdFromOutApply(row.id).then(value => {
                this.goodList = value;
            })
        },

        //审核跟踪
        showApprove(row) {
            this.dialogTableVisible = true;
            this.type = 1;
            this.comments = row.comments;
            this.refacReason = row.refacReason;
            this.refacLocation = row.refacLocation;
            this.approverTime = row.approverTime;
            this.approverComments = row.approverComments;
            this.approveStatus = row.approveStatus;
            this.odoCode = row.odoCode;
            if (row.approverTime !== null) {
                this.formatApproveTime = this.approverTime.replace("T", " ");
            }
            getGoodsByIdFromOutApply(row.id).then(value => {
                this.goodList = value;
            })
        },

        //审核通过
        pass() {
            this.approveInfo.createBy = this.createBy;
            this.approveInfo.odoCode = this.odoCode;
            this.approveInfo.approverComments = this.approverComments;
            this.approveInfo.approveStatus = 2;
            approveOutApply(this.approveInfo).then(value => {
                this.$message.success(value);
                this.dialogTableVisible = false;
                this.find();
            });
        },

        //审核不通过
        reject() {
            this.approveInfo.approverComments = this.approverComments;
            this.approveInfo.approveStatus = 0;
            approveOutApply(this.approveInfo).then(value => {
                this.$message.success(value);
                this.dialogTableVisible = false;
                this.find();
            });
        },
    },
    created() {
        this.find();
    }
};

export default outApplyApprove;